<template>
    <div>
        授权跳转
    </div>
</template>

<script setup lang="ts">
import LoginApi from '@/api/modules/LoginApi';

import menujson from '../../mock/menu.json'
import authjson from '../../mock/auth.json'
import { ElMessage } from 'element-plus';
import router from '@/router';
import { useAuthStore } from "@/store/modules/auth";
import { useMenuStore } from "@/store/modules/menu";
import UserApi from '@/api/modules/UserApi';
const authStore = useAuthStore()
const menuStore = useMenuStore()
const assertCode = async () => {
    let code = getQueryString('code')
    if (code != null) {
        let authData = await LoginApi.getAccessTokenByCode(code)
        // 获取 token  
        authStore.access_token = authData.data.access_token
        authStore.refresh_token = authData.data.refresh_token
        authStore.token_type = authData.data.token_type

        const userInfo =await  UserApi.userInfo()
        authStore.userInfo=  userInfo.data


        // 获取菜单  
        let menuData = menujson;
        if (menuData.data.menu.length === 0) {
            alert("当前用户无任何菜单权限，请联系系统管理员")
            return
        }
        const menus = await LoginApi.getMenusByScopes()
        console.log(JSON.stringify(menus))
        menuStore.list = menus.data
        menuStore.permissions = menuData.data.permissions
        router.replace({ path: '/' });
        ElMessage.success("Login Success 登录成功");
    } else {
        const params = window.location.search
        console.log(LoginApi.AUTHORIZE + params)
        window.location.replace(LoginApi.AUTHORIZE + params)
    }
}
assertCode()


/**
 * 获取地址栏参数
 * @param name 地址栏参数的key
 */
function getQueryString(name: string) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
    var r = window.location.search.substr(1).match(reg)
    if (r != null) {
        return unescape(r[2])
    }
    return null
}



</script>

<style lang="scss" scoped></style>